// 用户管理
$(function () {
    /* ------------------------------------------------------------------------------------------------------- */
    let page = document.getElementsByClassName("pagination")[0]
    let pagelist = page.getElementsByTagName("li")
    for (let i = 1; i <= pagelist.length; i++) {
        if (i == page.title) {
            pagelist[i - 1].className = "active"
        }
    }
    /* ------------------------------------------------------------------------------------------------------- */
    let btndanger = document.getElementsByClassName("btn-danger")
    for (let i = 0; i < btndanger.length; i++) {
        btndanger[i].onclick = (e) => {
            e.preventDefault();
            var data = {
                id: btndanger[i].getAttribute('delhref').split('?')[1].split('&')[0].split('=')[1],
                page: btndanger[i].getAttribute('delhref').split('?')[1].split('&')[1].split('=')[1]
            }
            // console.log(data);
            $.ajax({
                url: '/deleteauthor',
                type: 'get',
                data: data,
                dataType: 'json',
                success: (d) => {
                    console.log("success");
                    switch (d.code) {
                        case 1:
                            swal.fire({ title: d.msg, type: "error" })
                            break;
                        case 2:
                            swal.fire({ title: d.msg, type: "success" }).then(() => {
                                window.location.href = "/manageauthors?page= " + data.page
                            })
                            break;
                        case 8:
                            swal.fire({ title: d.msg, type: "error" })
                            break;
                    }
                },
                error: () => {
                    swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                        window.location.href = "/login"
                    })
                }
            })
        }

    }

    // 注册逻辑
    $("#tosignIn").click(e => {
        e.preventDefault()
        var formData = {
            author: $('#authorname').val(),
            password: $('#password').val(),
            email: $('#email').val(),
        }
        console.log(formData);
        $.ajax({
            url: '/addauthor',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: (d) => {
                switch (d.code) {
                    case 0:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '用户添加成功' }).then(() => {
                            window.location.href = "/manageauthors"
                        })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })
})